package com.sike.netty.jinjie.bytebuf.api;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.util.internal.StringUtil;

public class DuplicateTest {
    public static void main(String[] args) {
        ByteBuf originalByteBuf = ByteBufAllocator.DEFAULT.buffer(12,24);

        // 写入 9 个字符
        originalByteBuf.writeBytes(new byte[]{'a','b','c','d','e','f','g','h','i'});
        // 读取 4 个字符
        originalByteBuf.readInt();
        ByteBufPrintUtil.printByteBuf(originalByteBuf,"originalByteBuf");

        //产生一个切片
        ByteBuf duplicateByteBuf = originalByteBuf.duplicate();
        ByteBufPrintUtil.printByteBuf(originalByteBuf,"duplicateByteBuf");


        // originalByteBuf 写入数据
        originalByteBuf.setByte(7,'z');
        originalByteBuf.writeBytes(new byte[]{'j','k','l','m','n','o','p'});
        StringBuilder stringBuilder = ByteBufPrintUtil.getPrintBuilder(originalByteBuf,"originalByteBuf");
        ByteBufUtil.appendPrettyHexDump(stringBuilder,originalByteBuf);
        System.out.println(stringBuilder.toString());

        stringBuilder = ByteBufPrintUtil.getPrintBuilder(duplicateByteBuf,"duplicateByteBuf");
        ByteBufUtil.appendPrettyHexDump(stringBuilder,duplicateByteBuf);
        System.out.println(stringBuilder.toString());
    }
}
